﻿using Admin2024.Application.Contracts.User.Dto;
using Admin2024.Domain.Entity.System;
using Admin2024.Domain.IDomainService.System.IUserService;
using Admin2024.Domain.ObjValue;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin2024.Application.Contracts.User
{
    public class UserServer : IUserServer
    {
        private readonly IAuthenticationService _authenticationService;

        private readonly IMapper mapper;
        private readonly IJwtService jwt;

        public UserServer(IAuthenticationService authenticationService,IMapper mapper, IJwtService jwt)
        {
            _authenticationService = authenticationService;
            this.mapper = mapper;
            this.jwt = jwt;
        }
        //登录
        public async Task<DomainResult<LoginSucessDto>> Login(string username, string password)
        {
            var user = await _authenticationService.LoginUserAsync(username, password);
            if (!user.IsSuccess)
            {
                return DomainResult<LoginSucessDto>.Error(user.Message);
            }
            
            var loginSucessDto = new LoginSucessDto
            {
                Id = user.Data.Id,
                UserName = user.Data.Username,
                Token = jwt.GenerateToken(user.Data, "")
            };
            return DomainResult<LoginSucessDto>.Success(loginSucessDto);
        }

        public Task<DomainResult<AppUser>> Logout()
        {
            throw new NotImplementedException();
        }
        //注册
        public async Task<DomainResult<RegSucceedDto>> Register(string username, string password, string ConfirmPassword)
        {
            var user = await _authenticationService.RegisterUserAsync(username, password, ConfirmPassword);
            if (!user.IsSuccess)
            {
                return DomainResult<RegSucceedDto>.Error(user.Message);
            }

            var regSucceedDto = new RegSucceedDto
            {
                Id = user.Data.Id
            };

            return DomainResult<RegSucceedDto>.Success(regSucceedDto);
        }
    }
}
